# Load necessary libraries
library(dplyr)
library(tidyverse)
library(openxlsx) # For reading/writing Excel files
library(readxl) 
library(Rtsne) # t-SNE library
library(ggplot2) # For visualization
# Load built-in iris dataset
data(iris)
iris_df <- as.data.frame(iris)
head(iris_df)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
# Remove duplicate rows to avoid t-SNE error
iris_df <- distinct(iris_df)

# Extract the variables for t-SNE
sampled_var <- iris_df[, 1:4]

2D tSNE

# tSNE 2D
tsne_results <- Rtsne(sampled_var, 
                      perplexity = 30, 
                      eta = 1000, 
                      max_iter = 5000)

# Convert results to a dataframe
Y <- as.data.frame(tsne_results$Y)
colnames(Y) <- c("tSNE_1", "tSNE_2")

# Add class labels
Y$classlabels <- iris_df$Species

# Plot the t-SNE results
ggplot(Y, aes(x = tSNE_1, y = tSNE_2, col = classlabels)) + 
  geom_point() +
  labs(x = "t-SNE - 1", 
       y = "t-SNE - 2", 
       color = "Species") + 
  theme_classic()

3D tSNE

set.seed(123)  # reproducibility
tsne_results_3d <- Rtsne(sampled_var, 
                         perplexity = 30,  # Reduced perplexity to avoid error
                         eta = 200,  # Adjusted learning rate
                         max_iter = 500,  # Increased iterations for better convergence
                         dims = 3, 
                         verbose = TRUE)

# Convert t-SNE results to a dataframe
Y_3d <- as.data.frame(tsne_results_3d$Y)
colnames(Y_3d) <- c("tSNE_1", "tSNE_2", "tSNE_3")

# Add class labels
Y_3d$classlabels <- iris_df$Species

# Print first few rows
#head(Y_3d)
Interactive tSNE plot
# Install and load plotly for 3D visualization (if not installed, install with install.packages("plotly"))
library(plotly)

# Create an interactive 3D scatter plot
plot_ly(Y_3d, 
        x = ~tSNE_1, 
        y = ~tSNE_2, 
        z = ~tSNE_3, 
        color = ~classlabels, 
        colors = c("red", "blue", "green"), 
        type = "scatter3d", 
        mode = "markers") %>%
  layout(title = "3D t-SNE Visualization of Iris Dataset")